import Vue from 'vue'
import App from './App.vue'
import router from './router'
/*引入element-ui*/
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
/*引入axios*/
import axios from "axios";
/*引入qs*/
import qs from "qs";
import dataV from '@jiaminghi/data-view';
// 引入全局css
import './assets/scss/style.scss';
// 按需引入vue-awesome图标
import Icon from 'vue-awesome/components/Icon';
import 'vue-awesome/icons/chart-bar.js';
import 'vue-awesome/icons/chart-area.js';
import 'vue-awesome/icons/chart-pie.js';
import 'vue-awesome/icons/chart-line.js';
import 'vue-awesome/icons/align-left.js';
import echarts from 'echarts'
Vue.prototype.$echarts = echarts
// 全局注册
Vue.component('icon', Icon);
Vue.use(dataV);
//使用Element-UI组件
Vue.use(ElementUI)
// 配置axios,qs到Vue
Vue.prototype.axios = axios;
// 配置axios的基础路径为网关
Vue.prototype.axios.defaults.baseURL = "http://localhost:9000";
Vue.prototype.qs = qs;

//配置axios拦截请求，添加token头信息
axios.interceptors.request.use(
    config => {
      let accessToken = localStorage.getItem("access-token");
      let refreshToken = localStorage.getItem("refresh-token");
      console.log("accessToken:" + accessToken);
      console.log("refreshToken:" + refreshToken);
      if (accessToken && refreshToken) {
        //把localStorage的token放在Authorization里
        config.headers.Authorization = accessToken;
        config.headers.RefreshToken = refreshToken;
      }
      return config;
    },
    function(err) {
      console.log("失败信息" + err);
    }
);

//错误响应拦截
axios.interceptors.response.use(res => {
  console.log('拦截响应');
  console.log(res);
  if( res.data.status === 'OK'){
      //如果响应内容有token，就保存
      if(res.data.accessToken && res.data.refreshToken){
          console.log("accessToken：" ,res.data.accessToken )
          console.log("refreshToken：" ,res.data.refreshToken )
          localStorage.setItem("access-token",res.data.accessToken)
          localStorage.setItem("refresh-token",res.data.refreshToken)
      }else{
          localStorage.setItem("access-token","")
          localStorage.setItem("refresh-token","")
      }
      return res;
  }
  if( res.data.data === '没有权限,需要登录' ){
    console.log('没有权限,需要登录')
      window.alert('没有权限,需要登录')
      window.location.href = '/'
  }else{
      alert(res.data.data)
  }
})

Vue.config.productionTip = false

new Vue({
  router,
  render: function (h) { return h(App) }
}).$mount('#app')
